import sun.reflect.generics.tree.ClassSignature;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class demo1 {
   //*****24. 两两交换链表中的节点,循环模拟方法
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode newprve=new ListNode();
        newprve.next=head;
        ListNode prve=newprve;
        ListNode cur=prve.next;
        ListNode next=cur.next;
        ListNode nnext=next.next;
        while(cur!=null&&next!=null){
            // 交换节点
            prve.next=next;
            cur.next=nnext;
            next.next=cur;

            //修改指针
            prve=cur;
            cur=nnext;
            if(cur!=null)next=cur.next;
            if(next!=null)nnext=next.next;
        }
        return newprve.next;
    }
    public ListNode swapPirs1(ListNode head){
        if(head==null||head.next==null)return head;
        ListNode behind=swapPirs1(head.next.next);
        ListNode ret=head.next;
        ret.next=head;
        head.next=behind;
        return ret;
    }
}
